package com.shuhua.taskServer.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shuhua.common.result.Result;
import com.shuhua.pojo.dto.TeamCreateDTO;
import com.shuhua.pojo.entity.Team;
import com.shuhua.pojo.entity.TeamMember;
import com.shuhua.pojo.vo.TeamApplyVO;
import com.shuhua.pojo.vo.TeamDetailVO;
import com.shuhua.pojo.vo.TeamWithLeaderVO;
import com.shuhua.taskServer.service.TeamMemberService;
import com.shuhua.taskServer.service.TeamService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 班组控制器
 */
@RestController
@RequestMapping("/api/teams")
@RequiredArgsConstructor
public class TeamController {
    
    private final TeamService teamService;
    private final TeamMemberService teamMemberService;
    
    /**
     * 创建班组（需要管理员审核）
     * @param teamCreateDTO 班组创建DTO
     * @return 创建结果（状态为审核中）
     */
    @PostMapping
    public Result<Team> createTeam(@RequestBody @Valid TeamCreateDTO teamCreateDTO) {
        return teamService.createTeam(teamCreateDTO);
    }
    
    /**
     * 管理员审核班组
     * @param teamId 班组ID
     * @param state 审核状态（1=审核通过，2=审核不通过）
     * @param adminId 管理员ID
     * @return 审核结果
     */
    @PutMapping("/{teamId}/audit")
    public Result<Boolean> auditTeam(
            @PathVariable Long teamId,
            @RequestParam Integer state,
            @RequestParam Long adminId) {
        return teamService.auditTeam(teamId, state, adminId);
    }
    
    /**
     * 分页查询所有创建班组申请
     * @param current 当前页
     * @param size 每页大小
     * @param teamType 班组类型（可选）
     * @param state 审核状态（可选，0=审核中，1=审核通过，2=审核不通过）
     * @return 分页结果，包含申请人信息和班组信息
     */
    @GetMapping("/applies/page")
    public Result<Page<TeamApplyVO>> pageTeamApplies(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) Integer teamType,
            @RequestParam(required = false) Integer state) {
        return teamService.pageTeamApplies(current, size, teamType, state);
    }
    
    /**
     * 分页查询所有班组信息以及班组组长的信息
     * @param current 当前页
     * @param size 每页大小
     * @param teamType 班组类型（可选）
     * @param name 班组名称（可选，模糊查询）
     * @return 分页结果，包含班组信息和组长信息
     */
    @GetMapping("/with-leader/page")
    public Result<Page<TeamWithLeaderVO>> pageTeamsWithLeader(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) Integer teamType,
            @RequestParam(required = false) String name) {
        return teamService.pageTeamsWithLeader(current, size, teamType, name);
    }
    
    /**
     * 获取班组详情
     * @param teamId 班组ID
     * @return 班组详情
     */
    @GetMapping("/{teamId}")
    public Result<Team> getTeamDetail(@PathVariable Long teamId) {
        return teamService.getTeamDetail(teamId);
    }
    
    /**
     * 获取班组详情（包含成员信息）
     * @param teamId 班组ID
     * @return 班组详情VO
     */
    @GetMapping("/{teamId}/detail")
    public Result<TeamDetailVO> getTeamDetailWithMembers(@PathVariable Long teamId) {
        return teamService.getTeamDetailWithMembers(teamId);
    }
    
    /**
     * 获取工匠创建的班组列表
     * @param craftsmanId 工匠ID
     * @return 班组列表
     */
    @GetMapping("/craftsman/{craftsmanId}")
    public Result<Object> getCraftsmanTeams(@PathVariable Long craftsmanId) {
        return teamService.getCraftsmanTeams(craftsmanId);
    }
    
    /**
     * 获取班组成员列表
     * @param teamId 班组ID
     * @return 成员列表
     */
    @GetMapping("/{teamId}/members")
    public Result<List<TeamMember>> getTeamMembers(@PathVariable Long teamId) {
        return teamMemberService.getTeamMembers(teamId);
    }
    
    /**
     * 添加班组成员
     * @param teamId 班组ID
     * @param craftsmanId 工匠ID
     * @return 添加结果
     */
    @PostMapping("/{teamId}/members")
    public Result<TeamMember> addTeamMember(@PathVariable Long teamId, @RequestParam Long craftsmanId) {
        return teamMemberService.addTeamMember(teamId, craftsmanId);
    }
    
    /**
     * 分页查询班组列表
     * @param current 当前页
     * @param size 每页大小
     * @param teamType 班组类型（可选）
     * @param name 班组名称（可选，模糊查询）
     * @return 分页结果
     */
    @GetMapping("/page")
    public Result<Page<Team>> pageTeams(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) Integer teamType,
            @RequestParam(required = false) String name) {
        return teamService.pageTeams(current, size, teamType, name);
    }
    
    /**
     * 退出班组
     * @param teamId 班组ID
     * @param craftsmanId 工匠ID
     * @return 退出结果
     */
    @DeleteMapping("/{teamId}/members/{craftsmanId}")
    public Result<Boolean> quitTeam(@PathVariable Long teamId, @PathVariable Long craftsmanId) {
        return teamMemberService.quitTeam(teamId, craftsmanId);
    }
} 